//
//  FANEmoticonCell.m
//  Weibo - OC
//
//  Created by user on 16/5/2.
//  Copyright © 2016年 user. All rights reserved.
//

#import "FANEmoticonCell.h"
#import "FANEmoticon.h"
#import "FANEmoticonViewModel.h"
#import "NSString+Emoji.h"
#import "PopView.h"

//#warning 宏定义一定要放到 导入头文件的 上面, 会影响编译

//define this constant if you want to use Masonry without the 'mas_' prefix

#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax

#define MAS_SHORTHAND_GLOBALS

#import <Masonry/Masonry.h>

@interface FANEmoticonCell ()

@property (nonatomic, strong) NSMutableArray *emoticonButtons;

@property (nonatomic, strong) UILabel *label;

@property (nonatomic, strong) UIButton *delectedButton;

@property (nonatomic, strong) PopView *popView;

@end

@implementation FANEmoticonCell

- (PopView *)popView{
    
    if (!_popView) {
        
        _popView = [PopView popView];
        
    }
    
    return _popView;
}

- (UIButton *)delectedButton{
    
    if (!_delectedButton) {
        
        _delectedButton = [[UIButton alloc] init];
        
        [_delectedButton setImage:[UIImage imageNamed:@"compose_emotion_delete_highlighted"] forState:UIControlStateHighlighted];
        
        [_delectedButton setImage:[UIImage imageNamed:@"compose_emotion_delete"] forState:UIControlStateNormal];

        [_delectedButton addTarget:self action:@selector(didDelectedButton) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _delectedButton;
}

- (UILabel *)label{
    
    if (!_label) {
        
        _label = [[UILabel alloc] init];
        
        _label.text = @"最近使用表情";
        
        _label.textColor = [UIColor darkGrayColor];
        
        _label.font = [UIFont systemFontOfSize:13];
        
        _label.hidden = true;
    }
    
    return _label;
}

- (NSMutableArray *)emoticonButtons{
    
    if (!_emoticonButtons) {
        
        _emoticonButtons = [NSMutableArray array];
    }
    return _emoticonButtons;
}

- (void)setIndexPathF:(NSIndexPath *)indexPathF{
    
    _indexPathF = indexPathF;
    
    if (indexPathF.section == 0) {
        
        self.label.hidden = false;
        
    }else{
        
        self.label.hidden = true;
    }
    self.delectedButton.hidden = indexPathF.section == 0;

}

- (void)setEmoticonsArray:(NSArray *)emoticonsArray{
    
    _emoticonsArray = emoticonsArray;
    
    for (UIButton *button in self.emoticonButtons) {
        
        button.hidden = true;
    }
    
    for (int i = 0; i < self.emoticonsArray.count; i++) {
        
        FANEmoticonButton *button = self.emoticonButtons[i];
        
        FANEmoticon *emoticon = emoticonsArray[i];
        
        button.hidden = false;
        
        [button addTarget:self action:@selector(didEmoticonButton:) forControlEvents:UIControlEventTouchUpInside];
        
        button.emoticon = emoticon;
        
    }
}

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI{
    
    
    [self addEmoticonButton];
    
    [self.contentView addSubview:self.label];
    
    [self.contentView addSubview:self.delectedButton];
    
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    
    [self.contentView addGestureRecognizer:longPress];
    
}

- (void)longPress:(UILongPressGestureRecognizer *)gesture{
    
    CGPoint locaticon = [gesture locationInView:gesture.view];
    
    FANEmoticonButton *button = [self locaticonWithPoint:locaticon];
    
    if (button == nil) {
        
        [self.popView removeFromSuperview];
        
        return;
    }
    
    if (gesture.state == UIGestureRecognizerStateBegan || gesture.state == UIGestureRecognizerStateChanged) {
        
        if (self.popView.emoticonButton.emoticon == button.emoticon) {
            
            return;
        }
        
        UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
        
        CGRect rect = [button.superview convertRect:button.frame toView:window];
        
        CGPoint tempCenter = self.popView.center;
        
        tempCenter.x = CGRectGetMidX(rect);
        
        self.popView.center = tempCenter;
        
        CGRect tempRect = self.popView.frame;
        
        tempRect.origin.y = CGRectGetMaxY(rect) - self.popView.frame.size.height;
        
        self.popView.frame = tempRect;
        
        self.popView.emoticonButton.emoticon = button.emoticon;
        
        [window addSubview:self.popView];

        
    }else{
        
        self.popView.emoticonButton.emoticon = nil;
        
        [self.popView removeFromSuperview];
    }
    
}

- (FANEmoticonButton *)locaticonWithPoint:(CGPoint)locaticon{
    
    for (FANEmoticonButton *button in self.emoticonButtons) {
        
        if (CGRectContainsPoint(button.frame, locaticon)) {
            
            return button;
        }
        
    }
    
    return nil;
}

- (void)addEmoticonButton{
    
    for (int i = 0; i < 20; i++) {
        
        FANEmoticonButton *button = [[FANEmoticonButton alloc] init];
        
//        button.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1];
        
        [self.emoticonButtons addObject:button];
        
        [self.contentView addSubview:button];
        
    }
    
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    CGFloat w = self.contentView.frame.size.width / 7;
    
    CGFloat h = (self.contentView.frame.size.height - 25) / 3;
    
    for (int i = 0; i < self.emoticonButtons.count; i++) {
                
        CGFloat numCol = i % 7;
        
        CGFloat numRow = i / 7;
        
        CGFloat x = numCol * w;
        
        CGFloat y = numRow * h;
        
        FANEmoticonButton *button = self.emoticonButtons[i];
        
        button.frame = CGRectMake(x, y, w, h);
        
    }
    
    
    self.delectedButton.frame = CGRectMake(self.contentView.frame.size.width - w, (self.contentView.frame.size.height - h) - 25, w, h);
    
    [self.label makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.contentView.centerX);
        
        make.bottom.equalTo(self.contentView.bottom).offset(-10);
        
    }];
    
}

- (void)didEmoticonButton:(FANEmoticonButton *)button{
    
//    self.emoticonBlock(button);
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FANDidEmoticonButtonNotification" object:nil userInfo:@{@"emoticon":button.emoticon}];
    
    [[FANEmoticonViewModel sharedEmoticon] saveRecentEmoticon:button.emoticon];
    
    UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
    
    PopView *popView = [PopView popView];
    
    CGRect rect = [button.superview convertRect:button.frame toView:window];
    
    CGPoint tempCenter = popView.center;
    
    tempCenter.x = CGRectGetMidX(rect);
    
    popView.center = tempCenter;
    
    CGRect tempRect = popView.frame;
    
    tempRect.origin.y = CGRectGetMaxY(rect) - popView.frame.size.height;
    
    popView.frame = tempRect;
    
    popView.emoticonButton.emoticon = button.emoticon;
    
    [window addSubview:popView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [popView removeFromSuperview];
    });
}

- (void)didDelectedButton{
    
//    NSLog(@"点击了删除按钮");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FANDidDelectedClickNotification" object:nil];
    
}

@end
